import os
import threading
import time
from datetime import datetime


def create_file_in_directory(directory):
    while True:
        # 获取当前时间，并格式化为字符串
        current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        # 构造文件名
        filename = "{}.txt".format(current_time)
        # 构造文件的完整路径
        filepath = os.path.join(directory, filename)

        # 尝试创建文件
        try:
            with open(filepath, 'w') as file:
                # 这里可以写入一些内容，或者留空
                pass
            # print(f"File {filepath} created successfully.")
            print("File {} created successfully.".format(filepath))
        except Exception as e:
            # print(f"Failed to create file {filepath}: {e}")
            print("Failed to create file {filepath}: {}".format(filepath,e))

            # 等待一秒
        time.sleep(1)

        # 设置目录路径


directory1 = "D:\\task_log"
directory2 = "D:\\task_log2"

# 创建并启动线程
thread1 = threading.Thread(target=create_file_in_directory, args=(directory1,))
thread2 = threading.Thread(target=create_file_in_directory, args=(directory2,))

# 启动线程
thread1.start()
thread2.start()

# 注意：这个脚本会无限循环地创建文件，直到你手动停止它（例如，通过Ctrl+C）